import type { RouteRecordRaw } from 'vue-router'

import { mergeRouteModules, traverseTreeValues } from '@nice/utils'

import { baseRoutes, fallbackNotFoundRoute } from './base'

const dynamicRouteFiles = import.meta.glob('./modules/**/*.ts', {
  eager: true,
})

/** 动态路由 */
const dynamicRoutes: RouteRecordRaw[] = mergeRouteModules(dynamicRouteFiles)

/** 基本路由列表，这些路由访问不需要权限拦截 */
const baseRouteNames = traverseTreeValues(baseRoutes, (route) => route.name)

/** 路由列表，由基本路由、外部路由和404兜底路由组成
 *  无需走权限验证（会一直显示在菜单中） */
const routes: RouteRecordRaw[] = [...baseRoutes, ...fallbackNotFoundRoute]
/** 有权限校验的路由列表，包含动态路由和静态路由 */
const accessRoutes = [...dynamicRoutes]

export { accessRoutes, baseRouteNames, routes }
